
<template>
  <jy-dialog
    title="税费设定"
    :visible.sync="dialogVisible"
    width="75%"
    show-close-tooltip
    top="5vh"
  >
    <div class="tax">
      <div class="top">
        <div class="left">
          <tax-header @changeTab="changeTab"></tax-header>
          <!--  :min-width="i.minWidth"  :cell-class-name="cellClassName"-->
          <jy-table :data="fundsTableData">
            <jy-column
              v-for="i in fundsHeader"
              :key="i.field"
              :field="i.field"
              :title="i.title"
              :align="i.align"
              :width="i.width"
              :minWidth="i.minWidth"
            >
              <!-- :state="row[i.field + 'state']" :precision="4"-->
              <template #default="{ row, rowIndex }">
                <jy-input-number
                  v-if="i.field !== 'a1'"
                  v-model="row[i.field]"
                  @blur="checkFunds(i.field, row[i.field], rowIndex)"
                  :min="0"
                  :title="row[i.field]"
                  size="mini"
                >
                  <!-- <span
                    slot="suffix"
                    v-if="row[i.field + 'state']"
                    @click="clearError(i.field, rowIndex)"
                  >
                    <jy-icon name="Gildata-color-error1"></jy-icon>
                  </span> -->
                </jy-input-number>
                <span v-else>{{ row[i.field] }}</span>
              </template></jy-column
            >
          </jy-table>
        </div>
        <div class="right">
          <tax-header
            title="债券交易手续费"
            @changeTab="changeTab1"
          ></tax-header>
          <jy-table :data="bondTableData">
            <jy-column
              v-for="i in bondHeader"
              :key="i.field"
              :field="i.field"
              :title="i.title"
              :align="i.align"
              :width="i.width"
              :minWidth="i.minWidth"
              size="mini"
            >
              <!-- :precision="getprecision(row[i.field])" -->
              <template #default="{ row, rowIndex }">
                <jy-input-number
                  v-if="i.field !== 'a1'"
                  :min="0"
                  @blur="checkBond(i.field, row[i.field], rowIndex)"
                  v-model="row[i.field]"
                  size="small"
                  :title="row[i.field]"
                ></jy-input-number>
                <span v-else>{{ row[i.field] }}</span>
              </template>
            </jy-column>
          </jy-table>
        </div>
      </div>
      <div class="bottom">
        <tax-header title="公司税费" :isTax="true"></tax-header>
        <jy-table :data="taxTableData">
          <jy-column
            v-for="i in TaxHeader"
            :key="i.field"
            :field="i.field"
            :title="i.title"
            :align="i.align"
            :minWidth="i.minWidth"
          >
            <!--  :precision="2" -->
            <template #default="{ row, rowIndex }">
              <jy-input-number
                v-if="i.field !== 'a1'"
                v-model="row[i.field]"
                :min="0"
                :max="100"
                :title="row[i.field]"
                @blur="checkTax(i.field, row[i.field], rowIndex)"
                :formatter="(value) => `${value}%`"
                :parser="(value) => value.replace('%', '')"
                size="mini"
              ></jy-input-number>
              <span v-else>{{ row[i.field] }}</span>
            </template>
          </jy-column>
        </jy-table>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <jy-button @click="dialogVisible = false" size="medium">取 消</jy-button
      ><jy-button type="primary" @click="confirmBtn" size="medium"
        >确 定</jy-button
      >
    </span>
  </jy-dialog>
</template>

<script>
import taxHeader from "./taxHeader.vue";
import {
  fundsTaxHeader,
  funds1TaxHeader,
  bondTaxHeader,
  TaxHeader,
  fundsTableData89,
  fundsTableData83,
  fundsTableData90,
  bondTableData89,
  bondTableData83,
  bondTableData90,
  taxTableData,
  bond1TaxHeader,
} from "../tableSetting.js";
import { getUserinfo, encode, decode } from "@/utils/native";
export default {
  components: {
    taxHeader,
  },
  computed: {
    fundsHeader() {
      return this.fundRadio == 89 ? this.fundsTaxHeader : this.funds1TaxHeader;
    },
    bondHeader() {
      return this.bondRadio == 89 ? this.bondTaxHeader : this.bond1TaxHeader;
    },
    fundsTableData() {
      return this["fundsTableData" + this.fundRadio];
    },
    bondTableData() {
      return this["bondTableData" + this.bondRadio];
    },
  },
  props: {},
  data() {
    return {
      editConfig: { trigger: "click", mode: "cell" },
      dialogVisible: false,
      fundsTaxHeader,
      funds1TaxHeader,
      bondTaxHeader,
      bond1TaxHeader,
      TaxHeader,
      fundRadio: 89,
      bondRadio: 89,
      fundsTableData89,
      fundsTableData83,
      fundsTableData90,
      bondTableData89,
      bondTableData83,
      bondTableData90,
      taxTableData,
      id: "",
    };
  },
  mounted() {},
  methods: {
    getprecision(val) {
      var numString = val.toString();
      var match = numString.match(/(?:\.\d*|(\d+))/);
      if (match[1]) {
        console.log("%c Line:182 🍔", "color:#ea7e5c", match[1].length);
        return match[1].length > 4 ? 4 : match[1].length;
      } else {
        return 0; // 如果没有小数部分，返回0
      }
    },
    showdialog() {
      this.dialogVisible = true;
      this.initTax();
    },
    changeTab(val) {
      this.fundRadio = val == "银行间" ? 89 : val == "上交所" ? 83 : 90;
    },
    changeTab1(val) {
      this.bondRadio = val == "银行间" ? 89 : val == "上交所" ? 83 : 90;
    },
    cellClassName({ column }) {
      if (column.property !== "a1") {
        return "bgColor";
      }
    },
    isPositiveFloat(value) {
      // 判断是否为正浮点数
      const pattern = /^\d+(\.\d+)?$/;
      return pattern.test(value) && parseFloat(value) > 0;
    },
    async confirmBtn() {
      this.dialogVisible = false;
      let data = {
        fundsTableData89: this.fundsTableData89,
        fundsTableData83: this.fundsTableData83,
        fundsTableData90: this.fundsTableData90,
        bondTableData89: this.bondTableData89,
        bondTableData83: this.bondTableData83,
        bondTableData90: this.bondTableData90,
        taxTableData: this.taxTableData,
      };
      const userInfo = await getUserinfo();
      let param = {
        type: this.id ? 3 : 4,
        pid: 48101,
        ucode: (userInfo && userInfo.userId) || 11801020,
        value: encode(JSON.stringify(data)),
        distinct: true,
        id: this.id ? this.id : undefined,
      };
    },
    async initTax() {
      const userInfo = await getUserinfo();
      let param = {
        type: 1,
        pid: 48101,
        ucode: (userInfo && userInfo.userId) || 11801020,
        distinct: true,
      };
      moduleSave(param).then((res) => {
        if (res.items.length) {
          this.id = res.items[0].id;
          const {
            fundsTableData89,
            fundsTableData83,
            fundsTableData90,
            bondTableData89,
            bondTableData83,
            bondTableData90,
            taxTableData,
          } = JSON.parse(decode(res.items[0].value));
          if (fundsTableData89) {
            this.fundsTableData89 = fundsTableData89;
            this.fundsTableData83 = fundsTableData83;
            this.fundsTableData90 = fundsTableData90;
            this.bondTableData89 = bondTableData89;
            this.bondTableData83 = bondTableData83;
            this.bondTableData90 = bondTableData90;
            this.taxTableData = taxTableData;
          }
        }
      });
    },
    checkTax(type, val, index) {
      if (!val) {
        this.taxTableData[index][type] = 0;
      } else {
        if (this.isPositiveFloat(val)) {
          var numString = val.toString();
          // 如果包含小数点
          if (numString.includes(".")) {
            // 使用正则表达式去掉末尾的零
            numString = numString.replace(/\.?0+$/, "");
          }
          // 将处理后的字符串转换回数字
          return parseFloat(numString);
        } else {
          this.taxTableData[index][type] = 0;
        }
      }
    },
    checkBond(type, val, index) {
      if (!val) {
        this["bondTableData" + this.fundRadio][index][type] = 0;
      }
    },
    checkFunds(type, val, index) {
      if (!val) {
        this["fundsTableData" + this.fundRadio][index][type] = 0;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.tax {
  height: calc(100vh - 220px);
  overflow: auto;
  .jy-input,
  .jy-input-number {
    width: 80%;
    display: inline-block;
    /deep/.jy-input__inner {
      text-align: right;
      padding-right: 20px;
      font-family: NumberFonts;
    }
  }
  /deep/.bgColor {
    background: #fffbe6;
  }
  .top {
    margin-right: 15px;
    display: flex;
    .right,
    .left {
      flex: 1;
      max-width: calc(50% - 10px);
    }
    .left {
      margin-right: 20px;
    }
  }
  .bottom {
    margin-right: 15px;
  }
}
</style>
